﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MF.UI.MFActivity;

namespace MF.UI
{
    public partial class IntroduceData : System.Web.UI.Page
    {

        public bool toEdit
        {
            get {
                if (Request.QueryString["ID"] != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.PopulateDDL();
                Calendar.SelectedDate = DateTime.Today;
                if (toEdit)
                {
                    PopulateFields();
                }
            }
        }

        private void PopulateDDL()
        {
            try
            {
                MFTransaction.IMFTransaction serv = new MFTransaction.MFTransactionClient();

                dropDownTransaction.DataSource = serv.GetAllTransaction();
                dropDownTransaction.DataBind();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void PopulateFields()
        {
            MFActivity.IMFActivity service = new MFActivity.MFActivityClient();
            long ID = Convert.ToInt64(Request.QueryString["ID"]);

            if (service.GetActivityItemById(ID) != null)
            {
                ActivityItem myItem = service.GetActivityItemById(ID);

                txtBoxDescription.Text = myItem.Description;
                txtBoxAmount.Text = myItem.Amount.ToString();
                chkboxInOut.Checked = myItem.InOut;
                dropDownTransaction.SelectedValue = myItem.TransactionId.ToString();
                Calendar.SelectedDate = myItem.TDate;
            }
        }

        private void ClearFields()
        {
            txtBoxDescription.Text = string.Empty;
            txtBoxAmount.Text = string.Empty;
            chkboxInOut.Checked = false;
            dropDownTransaction.SelectedValue = "1";
            Calendar.SelectedDate = DateTime.Today;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ActivityItem toSubmit = new ActivityItem();

                    toSubmit.Description = txtBoxDescription.Text;
                    toSubmit.Amount = Convert.ToDecimal(txtBoxAmount.Text);
                    toSubmit.InOut = chkboxInOut.Checked;
                    toSubmit.TransactionId = Convert.ToInt64(dropDownTransaction.SelectedValue);
                    toSubmit.TDate = Calendar.SelectedDate;
        
                MFActivity.IMFActivity service = new MFActivity.MFActivityClient();
                service.Insert(toSubmit);

                ClearFields();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}